<template>
    <div class="content">
        <el-row :gutter="20">
            <el-col :span="12">
                <el-button type="primary" :icon="CirclePlus" @click="onInsert">添加</el-button>
            </el-col>
            <el-col :span="12" class="text-right">
                <el-input v-model="form.search" style="width: 240px" placeholder="请输入关键词" />
                <el-button type="primary" class="ml10" :icon="Search" @click="onSearch">搜索</el-button>
                <el-button type="primary" class="ml10" :icon="RefreshRight" @click="onSearch('Refresh')" />
            </el-col>
        </el-row>
        <h1 class="text-center">项目汇总表</h1>
        <el-table class="mt20" :data="tableData" border v-loading="form.loading" style="width: 100%;font-size: 16px;" @cell-dblclick="onCellDb">
            <el-table-column type="index" align="center" :index="indexMethod" label="序号" width="60" />
            <el-table-column label="项目名称" align="center" show-overflow-tooltip >
                <template #default="scope">
                    <div class="text-left"><el-link type="primary" @click="onTitle(scope.row)">{{scope.row.title}}</el-link></div>
                </template>
            </el-table-column>
            <el-table-column prop="manager" align="center" label="项目经理" width="160"/>
            <el-table-column prop="price" align="center" label="合同金额" width="160"/>
            <el-table-column prop="settlement" align="center" label="结算金额" width="160"/>
            <el-table-column prop="start_date" align="center" label="开工日期" width="150"/>
            <el-table-column prop="end_date" align="center" label="竣工日期" width="150" />
            <el-table-column prop="work_period" align="center" label="合同工期" width="100" />
            <el-table-column prop="build_date" align="center" label="已施工天" width="100" />
            <el-table-column prop="remaining_date" align="center" label="剩余天" width="100" />
            <el-table-column prop="status" align="center" label="项目状态" width="120" />
            <el-table-column fixed="right" align="center" label="操作" width="150">
                <template #default="scope">
                    <el-link type="primary" @click="onEdit(scope.row)">查看</el-link>
                    <el-popconfirm title="确定要删除么?" @confirm="onDelete(scope.row.id)">
                        <template #reference>
                            <el-link type="danger" >删除</el-link>
                        </template>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <div class="mt20">
            <el-pagination v-model:current-page="form.current" v-model:page-size="form.limit" :page-sizes="[10, 20, 30, 40, 50, 100]" layout="total, sizes, prev, pager, next, jumper" :total="form.total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
        </div>
    </div>

</template>
<script lang="ts" setup>
import { ref,onMounted } from 'vue'
import router from '../../../router';
import { CirclePlus, RefreshRight, Search } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import http from '../../../util/request';
import useStore from '../../../store/index'

const useStores = useStore()

const indexMethod = (index: number) => {
  return index+= 1
}
const form = ref<any>({search:'',page:0,current:0,total:10,limit:20,loading:false,parentBorder:true})
    
interface Project {
    id: number
    title: string
    users: number
    created_at: string
    start_date: string
    end_date: string
    status: string
}
const tableData = ref<Project[]>([])

const onGetData = () => {
    console.log('请求')
    form.value.page = 0;
    form.value.loading = true
    http.post('/project',form.value).then((response:any)=>{
        form.value.loading = false
        response.data.data.forEach((e:any) => {
            e.build_date = onChangeDate(e.start_date,1)
            e.remaining_date = onChangeDate(e.end_date,2)
        });
        tableData.value = response.data.data
        form.value.total = response.data.total
    })
}

const onDelete = (id: number) => {
    http.delete('/project/'+id).then((response:any)=>{
        if(response.data.code != 200){
            ElMessage.error(response.data.msg);return;
        }
        ElMessage.success(response.data.msg);
        onGetData();
    })
}

const onChangeDate = (data:any,type:number) => {
    const _date:any = new Date(data);
    const currentDate:any = new Date(getCurrentDate())
    if(type == 1){
        const timeDiff = Math.abs(currentDate - _date);
        return Math.ceil(timeDiff / (1000 * 60 * 60 * 24));
    }

    if(type == 2){
        const timeDiff = Math.abs(_date - currentDate);
        if(_date < currentDate){
            return '-'+Math.ceil(timeDiff / (1000 * 60 * 60 * 24));
        }
        return Math.ceil(timeDiff / (1000 * 60 * 60 * 24));
    }
    
}

function getCurrentDate() {
  const date = new Date();
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始
  const day = String(date.getDate()).padStart(2, '0');
  return `${year}-${month}-${day}`;
}

const onSearch = (type = '') => {
    if(type == 'Refresh'){
        form.value.search = ''
        onGetData()
        return
    }
    if(!form.value.search){
        ElMessage.error('请输入搜索内容')
    }
    form.value.page = 1
    onGetData()
}


const onCellDb = (data: Project) => {
    console.log(data)
}

const onTitle = (data: Project) => {
    //项目信息写入缓存
    sessionStorage.setItem('project',JSON.stringify(data))
    useStores.selectProject(true)
    useStores.setProject(data)
    router.push({ name: 'projectInfo',params: { id: data.id }})
}

const onEdit = (data: Project) => {
    //项目信息写入缓存
    sessionStorage.setItem('project',JSON.stringify(data))
    useStores.selectProject(true)
    useStores.setProject(data)
    router.push({ name: 'projectEdit',params: { id: data.id }})
}

const onInsert = () => {
    router.push({ name: 'projectEdit',params: { id: 0 }})
}

const handleSizeChange = (val: number) => {
    form.value.limit = val
    onGetData()
}
const handleCurrentChange = (val: number) => {
    form.value.page = val
    onGetData()
}
onMounted(()=>{
    onGetData()
})

</script>

<style scoped>

</style>